/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hextd.tower;

import hextd.core.Context;
import hextd.core.ContextPlayer;
import hextd.hex.Hex;
import hextd.util.ParameterUtils;

/**
 *
 * @author hylke
 */
public class TowerHomeAI extends TowerHome {

	private int budgetStart = -1;
	private float incomeFactor = 1.0f;
	private int countPerType = 600;
	private int lastType = -1;
	private int lastTypeCount = 0;
	private int[] preferredTypes = {0, 1, 2, 3, 4, 5, 6, 7, 8};
	private int lastSize = -1;
	private int[] preferredSizes = {0, 1, 2};
	private int perBatch = 1;
	//
	private int lastIncome = 1;

	public TowerHomeAI(Context context, Hex hex, int owner, int activateTick) {
		super(context, hex, owner, activateTick);
		moneyTimerStart = 120;
	}

	private int getWantedCreepSize() {
		if (lastTypeCount <= 0) {
			lastSize++;
			lastSize = ParameterUtils.forceIndexBounds(preferredSizes, lastSize);
		}
		return ParameterUtils.forceIndexBounds(curSizes, preferredSizes[lastSize]);
	}

	private int getWantedCreepType() {
		if (lastTypeCount <= 0) {
			lastType++;
			lastType = ParameterUtils.forceIndexBounds(preferredTypes, lastType);
			lastTypeCount = countPerType;
		}
		return ParameterUtils.forceIndexBounds(curCreepTypes, preferredTypes[lastType]);
	}

	@Override
	public void runTick(int gameTime) {
		super.runTick(gameTime);
		ContextPlayer player = context.getPlayer(getOwner());
		long funds = player.getCredits(true);

		int up = getUpgradePrice(true);
		if (canUpgrade() && funds > up && (lastIncome > (up / 4) || (funds / 3) > up)) {
			player.queuePayment(up);
			queueUpgrade(gameTime + Context.actionDelay);
			funds = player.getCredits(true);
		}

		lastTypeCount--;
		int wantedSize = getWantedCreepSize();
		int wantedCreepType = getWantedCreepType();

		int wantedCreepCount = perBatch;
		if (wantedSize == 2) {
			wantedCreepCount = Math.max(wantedCreepCount, 10);
		}
		wantedCreepCount = Math.min(wantedCreepCount, spawnPoolMax / 3);
		if (getSpawnPoolSize() > wantedCreepCount) {

			int wantedPath = 0;
			int wantedCreepHealth = creepHealthsMax;
			boolean send = false;
			while (wantedCreepHealth >= 0 && !send) {
				long creepPrice = wantedCreepCount * getCreepPrice(wantedCreepHealth, wantedCreepType);
				if (creepPrice <= funds) {
					queueSendCreeps(
							gameTime + Context.actionDelay,
							wantedCreepType,
							wantedCreepCount,
							wantedSize,
							wantedPath,
							wantedCreepHealth,
							true);
					send = true;
				}
				wantedCreepHealth--;
			}
		}

	}

	@Override
	protected boolean runActivate() {
		ContextPlayer player = context.getPlayer(getOwner());
		player.resetCredits(budgetStart, 0);
		return super.runActivate();
	}

	@Override
	protected void giveIncome(int gameTime) {
		ContextPlayer player = this.context.getPlayer(this.getOwner());
		lastIncome = Math.round(incomeFactor * income);
		player.doReceive(lastIncome);
		player.setScore(income);
		//System.out.println("TowerHomeAI::runTick: Tick: " + gameTime + " Income for player " + this.getOwner() + " = " + lastIncome);
	}

	@Override
	public void setSetting(JsonTower.optionKey key, String value) {
		switch (key) {
			case incomedelay:
				moneyTimerStart = Integer.parseInt(value);
				break;

			case incomestart:
				income = Integer.parseInt(value);
				break;

			case incomefactor:
				incomeFactor = Float.parseFloat(value);
				break;

			case budget:
				budgetStart = Integer.parseInt(value);
				break;

			case creeptypes:
				String[] split = value.split(",");
				int[] numbers = new int[split.length];
				for (int i = 0; i < split.length; i++) {
					numbers[i] = Integer.parseInt(split[i]);
				}
				preferredTypes = numbers;
				break;

			case creepticks:
				countPerType = Integer.parseInt(value);
				break;

			case creepsizes:
				split = value.split(",");
				numbers = new int[split.length];
				for (int i = 0; i < split.length; i++) {
					numbers[i] = Integer.parseInt(split[i]);
				}
				preferredSizes = numbers;
				break;

			default:
				super.setSetting(key, value);
		}
	}
}
